using System;

namespace codebase.UI
{
    public class Button : Control
    {
        public int NotPressedColor = ColorExtensions.RED16;
        public int PressedColor = ColorExtensions.GREEN16;
        public int ForeColor = ColorExtensions.BLACK16;
        public int BorderColor = ColorExtensions.BLACK16;

        public int Padding = 1;

        String _Text;

        public bool isPressed = false;
        public bool isStateButton = false;

        public String Text
        {
            get { return _Text; }
            set
            {
                string[] lines = value.Split('\n');

                Width = 0;
                foreach (string s in lines)
                {
                    if (Width < s.Length * Parent.Parent.screen.CharacterWidth) Width = s.Length * Parent.Parent.screen.CharacterWidth;
                }

                Height = lines.Length * Parent.Parent.screen.CharacterHeight;

                _Text = value;
            }
        }

        public delegate void ButtonPressedDelegate(Button btn);

        public event ButtonPressedDelegate Pressed;

        internal void DoButtonPressed()
        {
            if (Pressed != null) Pressed(this);
        }

        public delegate void ButtonReleasedDelegate(Button btn);

        public event ButtonReleasedDelegate Released;

        internal void DoButtonReleased()
        {
            if (Released != null) Released(this);
        }

        public Button(int x, int y, String txt, Window parent)
        {
            Init(x, y, txt, parent);
        }

        void Init(int x, int y, String txt, Window parent)
        {            
            Parent = parent;

            Left = x * Parent.Parent.screen.CharacterWidth;
            Top = y * Parent.Parent.screen.CharacterHeight;

            string[] lines = txt.Split('\n');

            Width = 0;
            foreach (string s in lines)
            {
                if (Width < s.Length * Parent.Parent.screen.CharacterWidth) Width = s.Length * Parent.Parent.screen.CharacterWidth;
            }

            Height = lines.Length * Parent.Parent.screen.CharacterHeight;

            Text = txt;
        }

        protected override void DrawAll()
        {
            Draw();
        }

        protected override void Draw()
        {
            int tw = _Text.Length * Parent.Parent.screen.CharacterWidth;
            int th = Parent.Parent.screen.CharacterHeight;
            int tx = Width / 2 - tw / 2;
            int ty = Height / 2 - th / 2;

            Parent.Parent.screen.DrawFilledRectangle(Left + Parent.Left, Top + Parent.Top , Left + Width + Parent.Left, Top + Height + Parent.Top, BackgroundColor);

            if (isPressed)
            {
                Parent.Parent.screen.DrawFilledRectangle(Left + Parent.Left + Padding, Top + Parent.Top + Padding, Left + Width + Parent.Left - Padding, Top + Height + Parent.Top - Padding, PressedColor);
            }
            else
            {
                Parent.Parent.screen.DrawFilledRectangle(Left + Parent.Left + Padding, Top + Parent.Top + Padding, Left + Width + Parent.Left - Padding, Top + Height + Parent.Top - Padding, NotPressedColor);
            }

            Parent.Parent.screen.DrawText(_Text, Left + Parent.Left + tx, Top + Parent.Top + ty, true, 0, ForeColor);
            Parent.Parent.screen.DrawRectangle(Left + Parent.Left + Padding, Top + Parent.Top + Padding, Left + Width + Parent.Left - Padding, Top + Height + Parent.Top - Padding, BorderColor);
        }

#if !PC
        public override void touchUp(object o, TouchEventArgs e)
        {
            if (IsPointInControl(e.x, e.y))
            {
                if (!isStateButton)
                {
                    if (isPressed)
                    {
                        isPressed = false;
                        Draw();
                        DoButtonReleased();
                    }
                }
                else
                {
                    DoButtonReleased();
                }
            }
            else
            {
                if (!isStateButton)
                {
                    if (isPressed)
                    {
                        isPressed = false;
                        Draw();
                    }
                }
            }
        }

        public override void touchDown(object o, TouchEventArgs e)
        {
            if (IsPointInControl(e.x, e.y))
            {
                if (isStateButton)
                {
                    isPressed = !isPressed;
                }
                else
                {
                    isPressed = true;
                }
                
                Draw();
                DoButtonPressed();
            }
        }

#endif
    }

}
